#ifndef _TCPCONNECTION_H
#define _TCPCONNECTION_H
#include "Socket.h"
#include "SocketIO.h"
#include "InetAddress.h"
#include "EventLoop.h"
#include <memory>
#include <my_head.h>
#include <functional>
using std::function;
using std::shared_ptr;

class TcpConnection : public std::enable_shared_from_this<TcpConnection>
{
    using TcpConnectionPtr = shared_ptr<TcpConnection>;
    using Callback = function<void(TcpConnectionPtr)>;

public:
    TcpConnection(int,EventLoop * loop);
    string receive();
    void send(const string &);
    void shutdown();
    bool isClosed();
    string toString();
    InetAddress getlocalAddr();
    InetAddress getpeerAddr();
    void setAllCallbacks(const Callback &, const Callback &, const Callback &);
    void handleNewConnectionCallback();
    void handleCloseCallback();
    void handleMsgCallback();
    void sendinLoop(const string &msg);

private:
    Socket _sock;
    InetAddress _localAddr;
    InetAddress _peerAddr;
    SocketIO _socketIO;
    Callback _onConnection;
    Callback _onMessage;
    Callback _onClose;
    EventLoop * _loop;
};

#endif
